#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e6 + 15;
const int M = 2e3 + 15;
const int MOD = 998244353;

mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int n, m, num;
struct Node {
  int op, p, k;
  bool operator<(const Node& o) const { return p < o.p; }
} a[M];
int k[M], kcnt;
ll dp[M][M];
ll fac[N], inv[N];

ll fastpow(ll b, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1) res = res * b % MOD;
    b = b * b % MOD;
    p >>= 1;
  }
  return res;
}
void init(int n) {
  fac[0] = inv[0] = 1;
  rep(i, 1, n) fac[i] = fac[i - 1] * i % MOD;
  inv[n] = fastpow(fac[n], MOD - 2);
  per(i, n - 1, 1) inv[i] = inv[i + 1] * (i + 1) % MOD;
}
ll C(int n, int m) {
  return n < m || m < 0 ? 0 : fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}
ll f(int l, int r, int k) {  // b[l] b[r] <= k
  if (l > r) return 1ll;
  if (r <= k + 1)
    return fac[r] * inv[l - 1] % MOD;
  else if (l >= k + 1)
    return fastpow(k + 1, r + 1 - l);
  else
    return fac[k] * inv[l - 1] % MOD * fastpow(k + 1, r - k) % MOD;
}
void solve() {
  cin >> n >> m;
  num = kcnt = 0;
  rep(i, 1, m) {
    int x, l, r;
    cin >> x >> l >> r;
    a[++num] = Node{0, l, x};
    if (r + 1 <= n) a[++num] = Node{1, r + 1, x};
  }
  a[++num] = Node{0, n, n - 1};
  rep(i, 1, num) k[++kcnt] = a[i].k;
  sort(k + 1, k + kcnt + 1);
  kcnt = unique(k + 1, k + kcnt + 1) - k - 1;
  sort(a + 1, a + num + 1);
  rep(j, 0, kcnt) dp[0][j] = 1;
  rep(i, 1, num) {
    int l = a[i - 1].p + 1, r = a[i].p;
    rep(j, 0, kcnt) dp[i][j] = dp[i - 1][j] * f(l, r, k[j]) % MOD;
    int x = lower_bound(k + 1, k + kcnt + 1, a[i].k) - k;
    if (a[i].op == 0) {  // b[p] <= k
      rep(j, x + 1, kcnt) dp[i][j] = dp[i][x];
    } else {
      rep(j, x + 1, kcnt) dp[i][j] = (dp[i][j] - dp[i][x] + MOD) % MOD;
      rep(j, 0, x) dp[i][j] = 0;
    }
    // cout << i << ' ' << a[i].p << ' ' << dp[i][kcnt] << endl;
  }
  cout << dp[num][kcnt] << endl;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init(N - 2);
  // freopen("data.in", "r", stdin);
  // freopen("data.out", "w", stdout);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}